package jsyf.oa.web.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportExcel;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.wreport.WReportCostFacade;

/**
 * Created by Administrator on 2017/12/29 0029.
 */
@Api(value = "费用明细接口", description = "费用明细接口")
@Controller
@RequestMapping("/wreportCost")
public class WReportCostController {
    @Resource
    private WReportCostFacade wReportCostFacade;

    @RequestMapping(value = "/config.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String costIds = jsonParam.getString("costIds");
        String start = jsonParam.getString("start");
        Boolean result = this.wReportCostFacade.config(costIds, Integer.valueOf(start));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(String.valueOf(result)));
    }

    @RequestMapping(value = "/downExcel.json")
    public void outFile(HttpServletResponse response, @RequestParam String userId, @RequestParam String reportMonth) throws Exception {
        //查出来的数据
        List<JSONObject> jsonObjectList = wReportCostFacade.getByUserIdAndReport(userId, reportMonth);
        getDownExcel(jsonObjectList, response);

    }

    @RequestMapping(value = "/downExcelServe.json")
    public void downExcelServe(HttpServletResponse response, @RequestParam String departId, @RequestParam String reportMonth) throws Exception {
        //查出来的数据
        List<JSONObject> jsonObjectList = wReportCostFacade.getByDepartIdAndReport(departId, reportMonth);
        getDownExcel(jsonObjectList, response);
    }

    @RequestMapping(value = "/getByCondition.json", method = RequestMethod.POST)
    public void getByCondition(HttpServletResponse response, @RequestBody String jsonData) throws Exception {
/*
        //查出来的数据
        List<JSONObject> records = wReportCostFacade.getByCondition(jsonData);
        String result = "";
        if (records != null && records.size() > 0) {
            result = BaseUtils.succsessArrRespond(records);
        } else {
            result = BaseUtils.succsessArrRespond();
        }
        ResponseUtils.putJsonResponse(response, result);*/
    }

    @RequestMapping(value = "/getDownByCondition.json")
    public void getDownByCondition(HttpServletResponse response, @RequestParam String userId, @RequestParam String beginTime, @RequestParam String endTime, @RequestParam String costIds, @RequestParam String type) throws Exception {
        List<JSONObject> records = wReportCostFacade.getByCondition(userId, beginTime, endTime, costIds, type);
        getDownExcel(records, response);
    }


    public void getDownExcel(List<JSONObject> jsonObjectList, HttpServletResponse response) throws Exception {
        //导出表名、列名
        String title = "销售费用列表";
        String[] rowsName = new String[]{"姓名", "电话", "报告日期", "一级费用名称", "二级费用名称", "金额(元)",
                "是否有发票", "备注"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = jsonObject.get("userName");
            objs[1] = jsonObject.get("tel");
            objs[2] = jsonObject.get("reportDate");
            objs[3] = jsonObject.get("costBigName");
            objs[4] = jsonObject.get("costSmallName");
            objs[5] = jsonObject.get("money");
            objs[6] = "1".equals(jsonObject.get("invoiceFlag")) ? "有" : "无";
            objs[7] = jsonObject.get("remark");
            dataList.add(objs);
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList, response);
        ex.export();
    }

    @ResponseBody
    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "更改审核状态",
            notes = "更改审核状态 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'id':'','userId':'','auditFlag':'','advise':''}", dataType = "String", paramType = "body")
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        boolean result = wReportCostFacade.applyAudit(jsonObject.getString("id"), jsonObject.getString("userId"), jsonObject.getString("auditFlag"), jsonObject.getString("advise"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }

    }

    @ResponseBody
    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "提交申请",
            notes = "提交申请 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{\n" +
            "\n" +
            "\t\"cost\": {\n" +
            "\t\t\"hotelName\": \"八天\"\n" +
            "\t},\n" +
            "\t\"main\": {\n" +
            "\t\t\"costSmallId\": \"25\",\n" +
            "\t\t\"money\": \"200\",\n" +
            "\t\t\"createUserId\": \"201712161907457D928C48A847385C21\",\n" +
            "\t\t  \"createUserName\":\"\",\n" +
            "\t\t\"auditFlag\": \"0\"\n" +
            "\t}\n" +
            "} ", dataType = "String", paramType = "body")


    public void add(HttpServletResponse response, @RequestBody String jsonData) {
        String id = wReportCostFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @ResponseBody
    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "删除申请",
            notes = "删除申请 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'id':''}", dataType = "String", paramType = "body")
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = wReportCostFacade.removeById(jsonObject.getString("id"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));

    }

    @RequestMapping(value = "/getUnDoCount.json", method = RequestMethod.GET)
    public void getUnDoCount(HttpServletResponse response, @RequestParam String userId) {
        String count = wReportCostFacade.getUnDoCount(userId);
        JSONObject records = new JSONObject();
        records.put("count", count);
        if (count != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取详细信息",
            notes = "获取详细信息 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'id':''}", dataType = "String", paramType = "body")
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = wReportCostFacade.getById(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getBxByPage.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取费用报销列表",
            notes = "获取费用报销列表 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'currentUserId':''，'userId':''，beginTime:'',endTime:''，'requestPage':''，'pageSize':''}", dataType = "String", paramType = "body")
    public void getBxByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = this.wReportCostFacade.getBxByPage(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }

    }


    @ResponseBody
    @RequestMapping(value = "/getByPage.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取列表",
            notes = "获取列表 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'userId':''，'key':''，'costSmallName':''，'auditFlag':''，'costSmallName':''，'requestPage':''，'pageSize':''}", dataType = "String", paramType = "body")
    public void getByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String requestPage = obj.getString("requestPage");
        String pageSize = obj.getString("pageSize");
        String auditFlag = obj.getString("auditFlag");
        String key = obj.getString("key");
        String createUserName = obj.getString("createUserName");
        String costSmallName = obj.getString("costSmallName");
        if (StringUtils.isEmpty(userId)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("用户Id为空"));
            return;
        }
        if (StringUtils.isEmpty(requestPage)) {
            requestPage = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = Integer.MAX_VALUE + "";
        }
        JSONObject records = this.wReportCostFacade.getByPage(userId, key, auditFlag, costSmallName, createUserName, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }

    }

    @ResponseBody
    @RequestMapping(value = "/getCosterTop10ByCostName.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "抄送人驳回",
            notes = "抄送人驳回 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'costName':'','住宿费':''}", dataType = "String", paramType = "body")
    public void getCosterTop10ByCostName(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.wReportCostFacade.getCosterTop10ByCostName(jsonData);
        String result = "";
        if (records != null && records.size() > 0) {
            result = BaseUtils.succsessArrRespond(records);
        } else {
            result = BaseUtils.succsessArrRespond();
        }
        ResponseUtils.putJsonResponse(response, result);
    }


    @ResponseBody
    @RequestMapping(value = "/copierReturn.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "抄送人驳回",
            notes = "抄送人驳回 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'id':'','copierReturnReason':''}", dataType = "String", paramType = "body")
    public void copierReturn(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = wReportCostFacade.copierReturn(jsonObject.getString("id"), jsonObject.getString("copierReturnReason"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @ResponseBody
    @RequestMapping(value = "/getAllAuditList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取所有审核列表",
            notes = "获取所有审核列表 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{\n" +
            "\t\"userId\": \"201711271733404C9DA1461F2421F828\",\n" +
            "\t\"auditFlag\": \"6\",\n" +
            "\t\"flag\": \"临时费用\",\n" +
            "\t\"type\": \"代收代付\",\n" +
            "\t\"createUserName\": \"\",\n" +
            "\t\"pageType\": \"pay\",\n" +
            "\t\"requestPage\": \"1\",\n" +
            "\t\"pageSize\": \"30\"\n" +
            "}", dataType = "String", paramType = "body")
    public void getAllAuditList(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        //String companyId = obj.getString("companyId");
        String requestPage = obj.getString("requestPage");
        String flag = obj.getString("flag");
        String pageSize = obj.getString("pageSize");
        String auditFlag = obj.getString("auditFlag");
        String key = obj.getString("key");
        String type = obj.getString("type");
        String pageType = obj.getString("pageType");
        String createUserName = obj.getString("createUserName");
        if (StringUtils.isEmpty(userId)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("用户Id为空"));
            return;
        }
        if (StringUtils.isEmpty(requestPage)) {
            requestPage = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = Integer.MAX_VALUE + "";
        }
        JSONObject records = this.wReportCostFacade.getAllAutitPage(userId, key, auditFlag, type, createUserName, pageType, Integer.parseInt(requestPage), Integer.parseInt(pageSize), flag);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }

    }

    @RequestMapping(value = "/getCostSmallNameList.json", method = RequestMethod.POST)
    public void getCostSmallNameList(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> jsonObjectList = wReportCostFacade.getCostSmallNameList();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @ResponseBody
    @RequestMapping(value = "/getLastCost.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取上次费用信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'userId':'','costType':'','createTime':'2018-09-09 13:38:47'}", dataType = "String", paramType = "body")
    public void getLastCost(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        JSONObject result = wReportCostFacade.getLastCost(jsonObject.getString("userId"), jsonObject.getString("costType"), jsonObject.getString("createTime"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }
}
